package com.yeqian.config;

import com.yeqian.utils.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author : yeqian
 * @Date : 2021/1/12 21:24
 * redisUrl的拦截器
 */
@Component
public class RedisUrlCountInterceptor implements HandlerInterceptor {

    private Logger logger = LoggerFactory.getLogger(RedisUrlCountInterceptor.class);

    /**
     * 注入Reids工具类
     */
    @Autowired
    RedisUtil redisUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求的路径
        String uri = request.getParameter("id");
        logger.error("【进入】"+uri);
        // 每次访问uri都会计数+1
        redisUtil.incr("article:"+uri, 1);
        // 始终放行
        return true;
    }
}
